package com.popo.android.service;

import com.popo.android.HandClock;
import com.popo.android.LockActivity;
import com.popo.android.StartActivity;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.view.WindowManager;

public class LockServices extends Service {

	private KeyguardLock mKeylock;
	private KeyguardManager mKeyguard;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("LockServices.onBind["+intent.getAction()+"]");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		
	    mKeyguard = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
        mKeylock = mKeyguard.newKeyguardLock("Charger");
        mKeylock.disableKeyguard();
//        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
//        windowManager.
      
		
		System.out.println("LockServices.onCreate one2 ");
		IntentFilter intent = new IntentFilter(
				"android.intent.action.SCREEN_ON");
		intent.setPriority(android.content.IntentFilter.SYSTEM_HIGH_PRIORITY-1);
		registerReceiver(new LockReceiver(), intent);
		IntentFilter ointent = new IntentFilter(
				"android.intent.action.SCREEN_OFF");
		ointent.setPriority(android.content.IntentFilter.SYSTEM_HIGH_PRIORITY-1);
		registerReceiver(new LockReceiver(), ointent);
		super.onCreate();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		System.out.println("LockServices.onDestroy");
		super.onDestroy();
	}
}

class LockReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
			System.out.println(intent.getAction());
			Intent loctIntent = new Intent(context, HandClock.class);
			loctIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(loctIntent);
		}
		abortBroadcast();
	}

}
